package com.usian.controller;

import com.usian.pojo.Bumen;
import com.usian.pojo.DTO;
import com.usian.pojo.Function;
import com.usian.pojo.Staff;
import com.usian.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @RequestMapping("/findAll")
    public String findAll1(Model model){
        List<DTO> list = companyService.findAll();

        model.addAttribute("list",list);
        return "company";
    }

    @RequestMapping("/findAll2")
    public String findAll2(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        String username = (String)session.getAttribute("username");
        Staff staff = companyService.findByName(username);
        Integer bid = staff.getBid();
        List<DTO> blist = companyService.ById(bid);
        model.addAttribute("blist",blist);
        return "company2";
    }

    @RequestMapping("/toadd")
    public String toadd(Model model){
        List<Bumen> bumen = companyService.findBumen();
        List<Function> function = companyService.findFunction();
        model.addAttribute("bumen",bumen);
        model.addAttribute("function",function);
        return "addStaff";
    }

    @RequestMapping("/add")
    public String add(Staff staff){
        companyService.add(staff);
        return "redirect:/company/findAll";
    }

    @RequestMapping("/del")
    public String del(Integer sid){
        companyService.delete(sid);
        return "redirect:/company/findAll";
    }

    @RequestMapping("/toupdate")
    public String toupdate(Integer sid,Model model){
        Staff staff = companyService.findById(sid);
        List<Bumen> bumen = companyService.findBumen();
        List<Function> function = companyService.findFunction();
        model.addAttribute("bumen",bumen);
        model.addAttribute("function",function);
        model.addAttribute("staff",staff);
        return "updateStaff";
    }

    @RequestMapping("/update")
    public String update(Staff staff){
        companyService.update(staff);
        return "redirect:/company/findAll";
    }




}
